using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    public partial class WorkCategoryEntry : BasePage_SOM
    {
        /// <summary>
        /// Author:SQB
        /// </summary>
       
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140805";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
            }
            
        }

        private void SetPageLoad()
        {
            if (this.Request["AdmRegCategoryID"] != null)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.GetWorkCategoryByID(this.LangCode, int.Parse(this.Request["AdmRegCategoryID"].ToString()));
                this.hidID.Value = dt.Rows[0]["AdmRegCategoryID"].ToString();
                this.txtCatEN.Text = dt.Rows[0]["Description1"].ToString();
                this.txtCatCH.Text = dt.Rows[0]["Description2"].ToString();
                this.txtCatHK.Text = dt.Rows[0]["Description3"].ToString();
                this.txtSequence.Text = dt.Rows[0]["Sequence"].ToString();//Add By Gordon 20150702

                this.chkSignReq.Checked = bool.Parse(dt.Rows[0]["FlagReqSign"].ToString());
                this.chkProtect.Checked = bool.Parse(dt.Rows[0]["FlagProtect"].ToString());
                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());
                this.hypToWorkCategory.NavigateUrl = "WorkSubCategoryList.aspx?AdmRegCategoryID=" + this.Request["AdmRegCategoryID"].ToString();
                if (this.chkVoid.Checked == true)
                {
                    AfterVoid();
                }
                dt.Dispose();
                dt = null;

            }
            else
            {
                this.hypToWorkCategory.Visible = false;
            }

            //Update By Gordon 20150702
            //Add One Message for U18
            //this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");

            this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','"+ this.GetMessage("SOM_SeqMandatory")+"','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_SeqMandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_SeqMandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //Update end

        }

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetLanguage()
        {
            this.lbl_CodeSetupTitle.Text = this.GetLabel("SOM_CodeSetupWorkCategoryEntry");
            this.lblTitleCategoryEntry.Text = this.GetLabel("SOM_WorkCategory");
            this.lblCatEN.Text = this.GetLabel("SOM_WorkCategoryDesc1", true);
            this.lblCatCH.Text = this.GetLabel("SOM_WorkCategoryDesc2", true);
            this.lblCatHK.Text = this.GetLabel("SOM_WorkCategoryDesc3", true);
            //Add By Gordon 20150702 U18
            this.lblSequence.Text = this.GetLabel("SOM_WCSequence",true);

            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnSaveAndReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveAndNew.Text = this.GetLabel("SOM_SaveNew");

            this.chkSignReq.Text = this.GetLabel("SOM_SignReq",true);
            this.chkProtect.Text = this.GetLabel("SOM_Proection",true);
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);
            this.btnCancel.Text = this.GetLabel("COM_Cancel");

            this.hypToWorkCategory.Text = this.GetLabel("SOM_ToWorkCategory");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WorkCategoryList.aspx");
        }

        private void Clear()
        {
            this.txtCatCH.Text = "";
            this.txtCatEN.Text = "";
            this.txtCatHK.Text = "";
            //Add By Gordon 20150702
            this.txtSequence.Text = "";
            this.chkVoid.Checked = false;
            this.chkSignReq.Checked = false;
            this.chkProtect.Checked = false;
        }

        /// <summary>
        /// CheckSeqUnique
        /// Add By Gordon 20150702 U18
        /// </summary>
        /// <returns></returns>
        private bool CheckSeqUnique()
        {
            return BLL.SOM.CodeSetup.CDAdmRegCategoryBLL.CheckUniqueSeq(int.Parse(txtSequence.Text), 1);
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id;
            if (this.chkVoid.Checked == false)
            {
                if (this.hidID.Value == String.Empty)
                {
                    if (CheckUnique(true))
                    {
                        //Add By Gordon 20150702 U18
                        if (CheckSeqUnique())
                        {
                            this.Alert(this.GetMessage("SOM_SeqUnique"));
                            return;
                        }

                        id = Save(0, 1);
                        this.hidID.Value = id.ToString();
                        this.hypToWorkCategory.Visible = true;
                        this.hypToWorkCategory.NavigateUrl = "WorkSubCategoryList.aspx?AdmRegCategoryID=" + id.ToString();
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        if (this.chkVoid.Checked == true)
                        {
                            AfterVoid();
                        }
                    }
                }
                else
                {
                    if (CheckUnique(false))
                    {
                        id = Save(int.Parse(this.hidID.Value), 2);
                        this.hypToWorkCategory.Visible = true;
                        this.hypToWorkCategory.NavigateUrl = "WorkSubCategoryList.aspx?AdmRegCategoryID=" + this.hidID.Value.ToString();
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        if (this.chkVoid.Checked == true)
                        {
                            AfterVoid();
                        }
                    }
                }
                
            }
            else
            {
                //Delete By Gordon 20150703 U18
                //bool flag = CheckVoid();
                //if (flag == true)
                //{

                    if (this.hidID.Value == String.Empty)
                    {
                        if (CheckUnique(true))
                        {
                            //Add By Gordon 20150702 U18
                            if (CheckSeqUnique())
                            {
                                this.Alert(this.GetMessage("SOM_SeqUnique"));
                                return;
                            }

                            id = Save(0, 1);
                            this.hidID.Value = id.ToString();
                            this.hypToWorkCategory.Visible = true;
                            this.hypToWorkCategory.NavigateUrl = "WorkSubCategoryList.aspx?AdmRegCategoryID=" + id.ToString();
                            if (this.chkVoid.Checked == true)
                            {
                                AfterVoid();
                            }
                            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        }
                    }
                    else
                    {
                        if (CheckUnique(false))
                        {
                            id = Save(int.Parse(this.hidID.Value), 2);
                            this.hypToWorkCategory.Visible = true;
                            this.hypToWorkCategory.NavigateUrl = "WorkSubCategoryList.aspx?AdmRegCategoryID=" + this.hidID.Value.ToString();
                            if (this.chkVoid.Checked == true)
                            {
                                AfterVoid();
                            }
                            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        }
                    }
                //Delete By Gordon 20150703 U18
                //}
                //else
                //{
                //    this.Alert(this.GetMessage("SOM_WorkCategoryVoidWarning"));
                //    this.chkVoid.Checked = false;
                //}

            }

        }


        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            int id;
            if (this.chkVoid.Checked == false)
            {
                if (this.hidID.Value == String.Empty)
                {
                    if (CheckUnique(true))
                    {
                        //Add By Gordon 20150702 U18
                        if (CheckSeqUnique())
                        {
                            this.Alert(this.GetMessage("SOM_SeqUnique"));
                            return;
                        }

                        id = Save(0, 1);
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));


                        string script = "<script language=javascript>function btnChange_Click(){window.location.href='WorkCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";

                        this.Page.RegisterStartupScript("aaa", script);

                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (CheckUnique(false))
                    {
                        id = Save(int.Parse(this.hidID.Value), 2);
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));


                        string script = "<script language=javascript>function btnChange_Click(){window.location.href='WorkCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";

                        this.Page.RegisterStartupScript("aaa", script);

                    }
                }
                
            }
            else
            {
                //Delete By Gordon 20150703 U18
                //bool flag = CheckVoid();
                //if (flag == true)
                //{

                    if (this.hidID.Value == String.Empty)
                    {
                        if (CheckUnique(true))
                        {
                            //Add By Gordon 20150702 U18
                            if (CheckSeqUnique())
                            {
                                this.Alert(this.GetMessage("SOM_SeqUnique"));
                                return;
                            }

                            id = Save(0, 1);
                            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                            this.Response.Redirect("WorkCategoryList.aspx");
                        }
                    }
                    else
                    {
                        if (CheckUnique(false))
                        {
                            id = Save(int.Parse(this.hidID.Value), 2);
                            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                            this.Response.Redirect("WorkCategoryList.aspx");
                        }
                    }
                //Delete By Gordon 20150703 U18
                //}
                //else
                //{
                //    this.Alert(this.GetMessage("SOM_WorkCategoryVoidWarning"));
                //    this.chkVoid.Checked = false;
                //}

            }

        }


        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int id;
            if (this.chkVoid.Checked == false)
            {
                if (this.hidID.Value == String.Empty)
                {
                    if (CheckUnique(true))
                    {
                        //Add By Gordon 20150702 U18
                        if (CheckSeqUnique())
                        {
                            this.Alert(this.GetMessage("SOM_SeqUnique"));
                            return;
                        }

                        id = Save(0, 1);
                        this.hidID.Value = "";
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        Clear();
                    }
                }
                else
                {
                    if (CheckUnique(false))
                    {
                        id = Save(int.Parse(this.hidID.Value), 2);
                        this.hidID.Value = "";
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        Clear();
                    }
                }

            }
            else
            {
                //Delete By Gordon 20150703 U18
                //bool flag = CheckVoid();
                //if (flag == true)
                //{

                    if (this.hidID.Value == String.Empty)
                    {
                        if (CheckUnique(true))
                        {
                            //Add By Gordon 20150702 U18
                            if (CheckSeqUnique())
                            {
                                this.Alert(this.GetMessage("SOM_SeqUnique"));
                                return;
                            }

                            id = Save(0, 1);
                            this.hidID.Value = "";
                            Clear();
                        }
                    }
                    else
                    {
                        if (CheckUnique(false))
                        {
                            id = Save(int.Parse(this.hidID.Value), 2);
                            this.hidID.Value = "";
                            Clear();
                        }
                    }
                    
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                //Delete By Gordon 20150703 U18
                //}
                //else
                //{
                //    this.Alert(this.GetMessage("SOM_WorkCategoryVoidWarning"));
                //    this.chkVoid.Checked = false;
                //}

            }

        }

        private void AfterVoid()
        {
            this.txtCatCH.Enabled = false;
            this.txtCatEN.Enabled = false;
            this.txtCatHK.Enabled = false;
            //Add By Gordon 20150702 U18
            this.txtSequence.Enabled = false;
            this.chkVoid.Enabled = false;
            this.chkSignReq.Enabled = false;
            this.chkProtect.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
        }

        private int Save(int admRegCategoryID,int action)
        {
             return MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.Update(
                                                    admRegCategoryID,
                                                    this.txtCatEN.Text.Trim(),
                                                    this.txtCatCH.Text.Trim(),
                                                    this.txtCatHK.Text.Trim(),
                                                    this.txtSequence.Text.Trim(),//Add By gordon 20150702
                                                    this.chkSignReq.Checked,
                                                    this.chkProtect.Checked,
                                                    this.ODMUser.ID,
                                                    DateTime.Now,
                                                    this.chkVoid.Checked,
                                                    this.ODMUser.ID,
                                                    DateTime.Now,
                                                    this.ODMUser.ID,
                                                    action
                                                    );
        }

 

        private bool CheckVoid()
        {
            if (this.Request["AdmRegCategoryID"] != null)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.GetWorkSubCategoryByWorkCategoryID(this.LangCode, int.Parse(this.Request["AdmRegCategoryID"].ToString()));
                if (dt.Select("FlagObsolete<>1").Length > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
                dt.Dispose();
                dt = null;
            }
            else
            {
                return true;
            }
       }


        private bool CheckUnique(bool insert)
        {
            if (insert == true)
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatEN.Text.Trim(), 1,-1).Rows.Count > 0 && this.txtCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatCH.Text.Trim(), 2,-1).Rows.Count > 0 && this.txtCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatHK.Text.Trim(), 3,-1).Rows.Count > 0 && this.txtCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
            else
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatEN.Text.Trim(), 1, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatCH.Text.Trim(), 2, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.CheckWorkCategoryUnqiue(this.txtCatHK.Text.Trim(), 3, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
        }
    }
}
